<?php
class TownsController extends AppController {

	var $name = 'Towns';
	var $helpers = array('Form');
	var $uses = array('Town', 'Metro');
	
	// Cookies:
	function beforeFilter() {
		$this->cookieMonster();
	}

	function index()
		{
			$metro = $this->Cookie->read('metro.id');
			if(!empty($metro)) {
				$this->set('viewTowns', $this->Town->inMetro($metro));
				$this->set('metro', $this->Metro->fetchMetro($this->Cookie->read('metro.id')));
			} else {
				$this->set('viewAllTowns', $this->Town->find('all'));
			}
			print_r($this->Cookie->read());
		}

	function add()
		{
		$this->set('metro', $this->Metro->fetchMetro($this->Cookie->read('metro.id')));
		$cookie = $this->Cookie->read('metro');
		if(!empty($cookie)) {
			if (!empty($this->data['Town']))
				{
					$this->data['Town']['ID'] = $this->Uuid->generate();
					$this->data['Town']['host_name'] = $this->makeSlug($this->data['Town']['public_name']);
					if($this->Town->save($this->data['Town']))
						{
							$this->flash('Your Town has been created.','/towns/');
						}
				}
			} else {
				$this->flash('Please select a metro before adding a town','/metros/');
			}
		}
	
	function modify($id)
		{
		$this->set('metro', $this->Metro->fetchMetro($this->Cookie->read('metro.id')));
		$cookie = $this->Cookie->read('metro');
		if(!empty($cookie)) {
			if (!empty($this->data['Town']))
				{
					$this->data['Town']['host_name'] = $this->makeSlug($this->data['Town']['public_name']);
					if($this->Town->save($this->data['Town']))
						{
							$this->flash('Your Town has been updated.','/towns/');
						}
				}
			} else {
				$this->flash('Please select a metro before adding a town','/metros/');
			}
		}
		
	function view() {
			$this->Town->name = $this->params['town-name'];
			$this->Town->info = $this->Town->fetchTownByName($this->params['town-name']);
			$this->set('data', $this->Town->info);
			$this->set('cookie_data', $this->Cookie->read());
			// Set or update the Town value of our cookie
			// $this->Cookie->write('town.id', $this->Town->info['Town']['ID'], true, '+2 days');
			print_r($this->Cookie->read());
			$this->set('cookie_data', $this->Cookie->read());
		}
		
	function delete($id) {
	
	}

}
?>
